package com.etc.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.entity.Forum;
import com.etc.entity.Message;
import com.etc.entity.Users;
import com.etc.entity.jsonui.JsonUi;
import com.etc.service.ForumService;
import com.etc.service.PersonService;
import com.etc.service.UsersService;
import com.etc.service.impl.ForumServiceImpl;
import com.etc.service.impl.PerviceServiceImpl;
import com.etc.service.impl.UsersServiceImpl;
import com.etc.util.PageData;
import com.google.gson.Gson;

/**
 * Servlet implementation class TousServlet
 */
@WebServlet("/ts.do")
public class TousServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ForumService forumService = new ForumServiceImpl();
	UsersService usersservice = new UsersServiceImpl();
	PersonService pService = new PerviceServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TousServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String op="query";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		
		
		if (op.equals("query")) {
			//查询数据库显示数据
			doTousQuery(request, response);
		}
	}
	private void doTousQuery(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		// 操作针对分页的操作
		// 三个参数 从页面来的
		int page = 1;
		int pageSize = 5;
		String strLike = "";
		if (request.getParameter("page") != null) {
			page = Integer.valueOf(request.getParameter("page"));
		}
		if (request.getParameter("limit") != null) {
			pageSize = Integer.valueOf(request.getParameter("limit"));
		}
		if (request.getParameter("strLike") != null) {
			strLike = request.getParameter("strLike");
		}

		
		if(request.getSession().getAttribute("username")!=null) {
			String sess=request.getSession().getAttribute("username").toString();
			System.out.println("session : "+sess);
			//转发用户的用户昵称和头像使他能够被调用
			String personImg = pService.SelPersonAll(sess).get(0).getPersonImg();
			String personNickName = pService.SelPersonAll(sess).get(0).getPersonNickName();
			request.setAttribute("personImg",personImg );
			request.setAttribute("personNickName",personNickName );
		}
		
		
		
//		//根据用户名查询房间号
//		String sess =request.getSession().getAttribute("username").toString();
//		System.out.println("session : "+sess);
//		//根据登入用户名进行房间号的查找
//		Users users = usersservice.SelUserDoorNo(sess);
//		System.out.println("users : "+users.getUserDoorNO());
//		
//		//转发用户的用户昵称和头像使他能够被调用
//		String personImg = pService.SelPersonAll(sess).get(0).getPersonImg();
//		String personNickName = pService.SelPersonAll(sess).get(0).getPersonNickName();
//		request.setAttribute("personImg",personImg );
//		request.setAttribute("personNickName",personNickName );
		
		PageData<Message> pageData = forumService.GetForumimg(page, pageSize, strLike);
		//System.out.println(""+pageData);
		//System.out.println("pagedata.size :" + pageData.getData().size());
		pageData.getData().forEach(System.out::println);
		request.setAttribute("pageData", pageData);
		request.setAttribute("pageDataSize", pageData.getData().size());
		request.setAttribute("strLike", strLike);
		try {
			request.getRequestDispatcher("community.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
//		PageData<Forum> pd = forumService.GetForumAll(page, limit, strLike);
		JsonUi<Message> jsonUi = new JsonUi<>(0,"",pageData.getTotalCount(),pageData.getData());
		
		Gson gson = new Gson();
		String string = gson.toJson(jsonUi);
		
		out.print(string);
		
		out.close();
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
